/**
 * {class} 框架按钮
 */
import executeInEditMode = cc._decorator.executeInEditMode;
import playOnFocus = cc._decorator.playOnFocus;
import O1HD, {managers} from "../../engines/gameEngine";
import {VIBRATE_TYPE} from "../../engines/services/miniGameContent/miniApp";

const {ccclass, property, requireComponent} = cc._decorator;

//描边颜色
export enum BT_LINE_COLOR {
    WHITE = 0,
    BLACK,
}

export enum BT_BODY_COLOR {
    NORMAL,
    OUTSTANDING,
    OBVIOUS,
}

@ccclass
@requireComponent(cc.Button)
@executeInEditMode()
@playOnFocus()
export default class btButton extends cc.Component {

    @property({type: cc.Enum(BT_LINE_COLOR), displayName: '按钮描边色', tooltip: '固定黑白'})//
    lineColor: BT_LINE_COLOR = BT_LINE_COLOR.WHITE;

    @property({type: cc.Enum(BT_BODY_COLOR), displayName: '按钮颜色', tooltip: 'NORMAL(普通按钮)\nOUTSTANDING(突出按钮)\nOBVIOUS(更突出、明显按钮)'})//
    bodyColor: BT_BODY_COLOR = BT_BODY_COLOR.NORMAL;

    @property({type: cc.Enum(BT_LINE_COLOR), displayName: '按钮文本颜色', tooltip: '固定黑白'})//
    textColor: BT_LINE_COLOR = BT_LINE_COLOR.WHITE;

    @property({displayName: '按钮文本', multiline: true})
    strButton: string = '按钮事件';

    onLoad() {
        this.OnUpdateLineColor();
        this.OnUpdateBodyColor();
        this.OnUpdateTextColor();
        this.node.on('click', () => {
            managers().soundMgr.playSound('mainSound/anNiu').then();
            managers().soundMgr.playVibrate();
        });
    }

    start() {

    }

    getButtonText() {
        return this.strButton;
    }

    setButtonText(text: string) {
        this.strButton = text;
        let bgNode = this.node.getChildByName('Background');
        if (bgNode) {
            let lbLabel = bgNode.getChildByName('Label');
            if (lbLabel) {
                lbLabel.getComponent(cc.Label).string = this.strButton;
            }
        }
    }

    OnUpdateLineColor() {
        switch (this.lineColor) {
            case BT_LINE_COLOR.WHITE: {
                this.node.color = cc.Color.WHITE;
                break;
            }
            case BT_LINE_COLOR.BLACK: {
                this.node.color = cc.Color.BLACK;
                break;
            }
        }
    }

    OnUpdateBodyColor() {
        let bgNode = this.node.getChildByName('Background');
        if (!bgNode) {
            console.error('#57错误 不存在的背景节点');
            return;
        }
        switch (this.bodyColor) {
            case BT_BODY_COLOR.NORMAL: {
                bgNode.color = cc.color(140, 208, 255);
                break;
            }
            case BT_BODY_COLOR.OUTSTANDING: {
                bgNode.color = cc.color(255, 211, 48);
                break;
            }
            case BT_BODY_COLOR.OBVIOUS: {
                bgNode.color = cc.color(230, 100, 76);
                break;
            }
        }
        let lbLabel = bgNode.getChildByName('Label');
        if (!lbLabel) {
            return;
        }
        lbLabel.getComponent(cc.Label).string = this.strButton;
    }

    OnUpdateTextColor() {
        let lbTextNode = cc.find('Background/Label', this.node);
        switch (this.textColor) {
            case BT_LINE_COLOR.WHITE: {
                lbTextNode.color = cc.Color.WHITE;
                break;
            }
            case BT_LINE_COLOR.BLACK: {
                lbTextNode.color = cc.Color.BLACK;
                break;
            }
        }
    }

    update(dt) {
        if (CC_EDITOR) {
            this.OnUpdateLineColor();
            this.OnUpdateBodyColor();
            this.OnUpdateTextColor();
        }
    }
}
